package vitrock.mae.app;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Locale;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.util.Log;

public class VitrockHelper extends SQLiteOpenHelper {
	private static final String DATABASE_PATH = "/data/data/mae.app/";
	private static final String DATABASE_NAME = "vitrock.db";
	private static final int SCHEMEA_VERSION = 1;
	private static final String WHITE_TABLE = "White";
	private static final String COLUMN_ID = " _id";
	private static final String COLUMN_PIECE = "Piece";
	private static final String COLUMN_VALUE = "Value";
	private static final String COLUMN_QUANTITY = "Quanity";
	private static final String COLUMN_CONDITION = "Condition";

	public SQLiteDatabase dbSqlite;

	private final Context myContext;

	public VitrockHelper(Context context, String name, CursorFactory factory,
			int version) {
		super(context, DATABASE_NAME, null, SCHEMEA_VERSION);
		this.myContext = context;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub

	}

	public void createDatabase() {
		createDB();
	}

	private void createDB() {
		boolean dbExist = DBExists();

		if (!dbExist) {
			this.getReadableDatabase();

			copyDBFromResource();
		}
	}

	private void copyDBFromResource() {

		InputStream inputStream = null;
		OutputStream outStream = null;
		String dbFilePath = DATABASE_PATH + DATABASE_NAME;

		try {
			inputStream = myContext.getAssets().open(DATABASE_NAME);

			outStream = new FileOutputStream(dbFilePath);

			byte[] buffer = new byte[1024];
			int length;
			while ((length = inputStream.read(buffer)) > 0) {
				outStream.write(buffer, 0, length);
			}
			outStream.flush();
			outStream.close();
			inputStream.close();
		} catch (IOException e) {
			throw new Error("Problem COPYING database from resource file.");
		}
	}

	public void openDatabase() throws SQLException {
		String myPath = DATABASE_PATH + DATABASE_NAME;
		dbSqlite = SQLiteDatabase.openDatabase(myPath, null,
				SQLiteDatabase.OPEN_READWRITE);
	}

	private boolean DBExists() {
		SQLiteDatabase db = null;

		try {
			String databasePath = DATABASE_PATH + DATABASE_NAME;
			db = SQLiteDatabase.openDatabase(databasePath, null,
					SQLiteDatabase.OPEN_READWRITE);
			db.setLocale(Locale.getDefault());
			db.setLockingEnabled(true);
			db.setVersion(1);
		} catch (SQLiteException e) {
			Log.e("SqlHelper", "database not found");
		}
		if (db != null) {
			db.close();
		}
		return db != null ? true : false;
	}

	@Override
	public synchronized void close() {
		if (dbSqlite != null) {
			dbSqlite.close();
		}
		super.close();
	}

	public Cursor getCursor() {
		SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
		queryBuilder.setTables(WHITE_TABLE);

		String[] asColumnsToReturn = new String[] { COLUMN_ID, COLUMN_PIECE,
				COLUMN_VALUE, COLUMN_QUANTITY, COLUMN_CONDITION };

		Cursor mCursor = queryBuilder.query(dbSqlite, asColumnsToReturn, null,
				null, null, null, null);

		return mCursor;
	}

	public String getPiece(Cursor c) {
		return (c.getString(1));
	}

	public String getValue(Cursor c) {
		return (c.getString(2));
	}

	public String getQuanity(Cursor c) {
		return (c.getString(3));
	}

}
